'use strict';

const Controller = require('egg').Controller;

class ConversationController extends Controller {

  async server() {
    const { ctx, app } = this;
    const nsp = app.io.of('/');
    const data = ctx.args[0] || {};
    try {
      const { info, conversation } = data;
      const { send_user_id, content, content_type } = info;
      const { conversation_id } = conversation;

      const newInfo = await ctx.service.conversationInfo.createConversationInfo(conversation_id, send_user_id, content, content_type);

      await ctx.service.conversation.setNewsContent(conversation_id, content, content_type);

      await nsp.emit(`conversation_${conversation_id}`, {
        type: 2,
        msg: '上传成功',
        new_info: newInfo,
      });

    } catch (e) {
      await nsp.emit('server', {
        msg: '出错了',
      });
    }
  }
}

module.exports = ConversationController;
